﻿/*
 * zproc_unittest.cxx
 *
 *  Created on: 2015年4月14日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zproc.h"
#include "zpsoe/zalgorithm/zstring.h"

using namespace zpsoe;
using namespace zpsoe::zalgorithm;

string format_path(const string &path) {
    return zstring::replace(path, "/", OS_SEPARATOR);
}

TEST(ZprocTest, current_work_dir) {
    string expected_("");
    EXPECT_TRUE(zproc::current_work_dir(expected_));

#ifdef CURRENT_WORK_DIR
    const string actual_(format_path(CURRENT_WORK_DIR));
    EXPECT_STREQ(actual_.c_str(), expected_.c_str());
#endif
}

TEST(ZprocTest, current_exe_path) {
    string expected_("");
    EXPECT_TRUE(zproc::current_exe_path(expected_));

#ifdef CURRENT_EXE_PATH
    const string actual_(format_path(CURRENT_EXE_PATH));
    EXPECT_STREQ(actual_.c_str(), expected_.c_str());
#endif
}

TEST(ZprocTest, current_exe_dir) {
    string expected_("");
    EXPECT_TRUE(zproc::current_exe_dir(expected_));

#ifdef CURRENT_EXE_DIR
    const string actual_(format_path(CURRENT_EXE_DIR));
    EXPECT_STREQ(actual_.c_str(), expected_.c_str());
#endif
}

TEST(ZprocTest, lock_proc_and_unlock_proc) {
#ifdef PLATFORM_WIN32
    EXPECT_FALSE(zproc::unlock_proc("test.lock"));
    EXPECT_TRUE(zproc::lock_proc("test.lock"));

    EXPECT_FALSE(zproc::lock_proc("test.lock"));
    EXPECT_TRUE(zproc::unlock_proc("test.lock"));
#else
    EXPECT_FALSE(zproc::unlock_proc("test.lock", "/tmp"));
    EXPECT_TRUE(zproc::lock_proc("test.lock", getppid(), "/tmp"));

    EXPECT_FALSE(zproc::lock_proc("test.lock", getppid(), "/tmp"));
    EXPECT_TRUE(zproc::unlock_proc("test.lock", "/tmp"));
#endif
}
